package com.tanhua.server.controller;

import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.domain.vo.UserInfoVoAge;
import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用设置模块
 */
@RestController
@RequestMapping("/users")
public class SettingsController {


    //注入SettingsService
    @Autowired
    private SettingsService usersSettings;

    /**
     * 通用设置--读取
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity getSettings() {
        //调用业务处理
        SettingsVo vo = usersSettings.querySettins();
        return ResponseEntity.ok(vo);
    }

    /**
     * 通用设置--保存
     *
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateNotification(@RequestBody SettingsVo vo) {
        //调用业务处理
        usersSettings.updateNotification(vo);
        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置 -- 黑名单分页查询
     *
     * @param page     当前页
     * @param pagesize 当前页数据条数
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(value = "page", defaultValue = "1") Long page,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Long pagesize) {
        //设置条件 防止无意义的查询
        page = page < 0 ? 1 : page; //如果页码是负数，查询第一页
        pagesize = pagesize < 0 ? 10 : pagesize;//如果每页数据是负数 查询十条
        pagesize = pagesize > 50 ? 50 : pagesize;//限制查询数据过大

        //调用业务
        PageResult<UserInfoVoAge> pageResult = usersSettings.blackList(page, pagesize);

        return ResponseEntity.ok(pageResult);
    }

    /**
     * 通用设置--移除黑名单
     *
     * @param blackUserId 黑名单id
     * @return
     */
    @DeleteMapping("//blacklist/{blackUserId}")
    public ResponseEntity removeBlackList(@PathVariable Long blackUserId) {
        usersSettings.removeBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置--保存陌生人问题信息
     *
     * @param paramMap
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map<String, String> paramMap) {

        usersSettings.updateQuestion(paramMap);

        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置--更改手机号--发送验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendValidateCode() {

        usersSettings.sendValidateCode();
        return ResponseEntity.ok(null);
    }

    /**
     * 通用设置--修改手机号--校验验证码
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> paramMap){

        Boolean flag = usersSettings.checkValidateCode(paramMap.get("verificationCode"));

        Map<String,Boolean> result = new HashMap<>();
        result.put("verification",flag);
        return ResponseEntity.ok(result);
    }

    /**
     * 通用设置--修改手机号--保存
     * @param paramMap
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity changeMobile(@RequestBody Map<String,String> paramMap,@RequestHeader("Authorization") String token){

        usersSettings.changeMobile(paramMap.get("phone"),token);
        return ResponseEntity.ok(null);


    }
}